<?php
/**
 * @version		$Id: helper.php 2011-08-21 vinaora $
 * @package		VINAORA NIVO SLIDER
 * @subpackage	mod_vt_nivo_slider
 * @copyright	Copyright (C) 2011 VINAORA. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 * @website		http://vinaora.com
 * @twitter		http://twitter.com/vinaora
 * @facebook	http://facebook.com/vinaora
 */

// no direct access
defined('_JEXEC') or die;

class modVT_Nivo_SliderHelper
{
	private function __construct(){}
	
	/*
	 * Add jQuery Library to <head> tag
	 */
	public static function addjQuery($source='local', $version='1.6.2'){
		
		if($source == 'local'){
			JHtml::script("media/mod_vt_nivo_slider/js/jquery/$version/jquery.min.js");
			return true;
		}
		
		if($source == 'google'){
			JHtml::script("https://ajax.googleapis.com/ajax/libs/jquery/$version/jquery.min.js");
			return true;
		}
		
		return false;

	}

	/*
	 * Add file [theme].css to <head> tag
	 */	
	public static function addThemCSS($theme, $layout='default'){
		switch($layout){
			case 'demo-standard':
				$theme = "default";
				break;
			case 'demo-pascal':
				$theme = "pascal";
				break;
			case 'demo-orman':
				$theme = "orman";
				break;
		}
		JHtml::stylesheet("media/mod_vt_nivo_slider/themes/$theme/$theme.css");
		return;
	}
	
	/*
	 * Get a Parameter in a Parameters String which are separated with a specify symbol (default: vertical bar '|').
	 * Example: Parameters = "value1 | value2 | value3". Return "value2" if positon = 2
	 */
	public static function getParam($params, $position=1, $separate='|'){

		$items = explode($separate, $params);

		if ( ($position > count($items)) || ($position<1) ) return NULL;

		return $items[$position-1];
	}
	
	/*
	 * 
	 */
	public static function getItems($params){

		$item_dir	= $params->get('item_dir');
		
		if ($item_dir == "-1") return NULL;
		
		$filter		= '([^\s]+(\.(?i)(jpg|png|gif|bmp))$)';
		$exclude	= array('.svn', 'CVS', '.DS_Store', '__MACOSX', '.htaccess');
		$excludefilter = array();
		array_push($excludefilter, $params->get('controlNavThumbsReplace'));
		
		$items		= JFolder::files(JPATH_ROOT.'/images/'.$item_dir, $filter, false, false, $exclude, $excludefilter);
		
		return $items;
	}
	
	public static function getLinks($params){
		$separate = "\n";
		$str = "";
		$item_dir = $params->get('item_dir');
		$items = self::getItems($params);
		
		if (empty($items) || !count($items)) return $str;
		
		$titles = $params->get('item_title');
		$links = $params->get('item_url');
		$target = $params->get('item_target');

		foreach($items as $i=>$value){

			$title	= self::getParam($titles, $i+1, $separate);
			$title	= trim($title);
			$title	= htmlspecialchars($title, ENT_QUOTES);
			
			$link	= self::getParam($links, $i+1, $separate);
			$link	= trim($link);
			$link	= htmlspecialchars($link, ENT_QUOTES);
			
			$src	= JURI::base(true)."/images/$item_dir/$value";
			$value	= str_replace(strtolower($params->get('controlNavThumbsSearch')), strtolower($params->get('controlNavThumbsReplace')), strtolower($value));
			$rel 	= JURI::base(false)."images/$item_dir/$value";
			
			if (!empty($link)){
				$str	.= "<a href=\"$link\" target=\"$target\">";
				$str	.= "<img src=\"$src\" alt=\"Vinaora Nivo Slider\" title=\"$title\" rel=\"$rel\" />";
				$str	.= "</a>";
			}
			else{
				$str	.= "<img src=\"$src\" alt=\"Vinaora Nivo Slider\" title=\"$title\" rel=\"$rel\" />";
			}
		}
		
		return $str."\n";
	}
	
	public static function getStartSlide($params){
		$startSlide = $params->get('startSlide');
		$total = count(self::getItems($params));
		
		if (!$startSlide){
			$startSlide = mt_rand(1, $total);
		}
		else{
			$startSlide = min($startSlide, $total);
		}
		
		return $startSlide-1;
	}
}
